package com.konne.nightmare.FastPublicOpinion.utils;

import android.app.Activity;

import com.konne.nightmare.FastPublicOpinion.ui.information.activity.MainActivity;

import java.util.Stack;


/**
 * Des:
 * Created by kele on 2020/11/19.
 * E-mail:984127585@qq.com
 */
public class ActivityManager {

    private ActivityManager() {
    }

    private static class ActivityMangerHolder {
        private static final ActivityManager INSTANCE = new ActivityManager();
    }

    public static ActivityManager getInstance() {
        return ActivityMangerHolder.INSTANCE;
    }

    private Stack<Activity> mStack = new Stack<>();

    public void add(Activity act) {
        mStack.add(act);
    }

    public void remove(Activity act) {
        act.finish();
        mStack.remove(act);
    }

    public void removeCurrent() {
        Activity act = mStack.lastElement();
        act.finish();
        mStack.remove(act);
    }

    public void clearAll() {
        for (int i = mStack.size() - 1; i >= 0; i--) {
            Activity act = mStack.get(i);
            act.finish();
            mStack.remove(act);
        }
    }

    public int size() {
        return mStack.size();
    }

    public MainActivity getMainAct() {
        for (Activity act : mStack) {
            if (act instanceof MainActivity) {
                return (MainActivity) act;
            }
        }
        return null;
    }
}
